#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i(a);i<=(b);++i)
#define req(i,a,b) for(int i(a);i>=(b);--i)
using namespace std;
signed main()
{
	freopen("indian.in","r",stdin);
	freopen("indian.out","w",stdout);
	ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
	int n,m; cin>>n>>m; vector<string> a(n);
	for(auto &x:a) cin>>x;
	vector<pair<int,int>> pos={{0,0},{1,2},{2,4},{3,1},{4,3}};
	vector<string> ans; int anscnt=1e18;
	rep(ox,0,4) rep(oy,0,4)
	{
		vector<string> res(n,string(m,'.'));
		rep(i,0,n-1) rep(j,0,m-1) if(a[i][j]=='#')
		{
			int ok=0,x=((i-ox)%5+5)%5,y=((j-oy)%5+5)%5;
			for(auto [px,py]:pos) if(x==px&&y==py) {ok=1;break;}
			if(ok) res[i][j]='1';
		}
		int dx[]={0,0,0,-1,1},dy[]={0,-1,1,0,0};
		vector<vector<int>> ctrl(n,vector<int>(m,0));
		rep(i,0,n-1) rep(j,0,m-1) if(res[i][j]=='1') rep(d,0,4)
		{
			int nx=i+dx[d],ny=j+dy[d];
		//	cerr<<"Flagged "<<nx<<" "<<ny<<" "<<((nx>=0&&nx<n&&ny>=0&&ny<n)?res[nx][ny]:'X')<<endl;
			if(nx>=0&&nx<n&&ny>=0&&ny<n) ctrl[nx][ny]=1/*,cerr<<"OK "<<nx<<" "<<ny<<endl*/;
		}
		rep(i,0,n-1) rep(j,0,m-1) if(a[i][j]=='#'&&!ctrl[i][j])
		{
			res[i][j]='1';
			//cerr<<"Flagged "<<i<<" "<<j<<endl;
			rep(d,0,4)
			{
				int nx=i+dx[d],ny=j+dy[d];
				if(nx>=0&&nx<n&&ny>=0&&ny<n) ctrl[nx][ny]=1;
			}
		} else if(a[i][j]=='#'&&res[i][j]=='.') res[i][j]='0';
	//	rep(i,0,n-1) cout<<res[i]<<endl;
		int cnt=0; rep(i,0,n-1) rep(j,0,m-1) cnt+=res[i][j]=='1';
		if(cnt<anscnt) anscnt=cnt,ans=res;
	//	cerr<<"Ended one turn"<<endl;
	}
	rep(i,0,n-1) cout<<ans[i]<<'\n';
	return 0;
}
